/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.core.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>(HexUtil)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/8/18
 */
public class HexUtil {
    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param hexString 16进制表示的字符串
     *
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int    len   = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    /**
     * 2进制转16进制
     *
     * @param bString 2进制字符串
     *
     * @return String
     */
    public static String binaryToHex(String bString) {
        if (bString == null || ObjectUtil.isEmpty(bString) || bString.length() % 8 != 0) {
            return null;
        }
        StringBuilder tmp  = new StringBuilder();
        int           iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4) {
            iTmp = 0;
            for (int j = 0; j < 4; j++) {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString().toUpperCase();
    }

    /**
     * 16进制转2进制
     *
     * @param hexString 十六进制字符串
     *
     * @return String
     */
    public static String hexToBinary(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0) {
            return null;
        }
        StringBuilder bString = new StringBuilder();
        String        tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString.append(tmp.substring(tmp.length() - 4));
        }
        return bString.toString();
    }

    /**
     * 16进制转2进制
     *
     * @param hexString 十六进制字符串
     *
     * @return String
     */
    public static String hexArrayBinary(String... hexString) {
        List<String> hexes = Arrays.asList(hexString);
        if (CollUtil.isEmpty(hexes)) {
            return null;
        }
        return hexes.stream().map(hex -> {
            String bString = "", tmp;

            for (int i = 0; i < hex.length(); i++) {
                tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hex.substring(i, i + 1), 16));
                bString += tmp.substring(tmp.length() - 4);
            }
            return bString;
        }).collect(Collectors.joining());
    }

    public static String[] hexStrToArray(String hexStr) {
        String[] array = new String[hexStr.length() / 2];
        int      k     = 2;
        for (int i = 0; i < array.length; i++) {
            array[i] = hexStr.substring(i * 2, k);
            k += 2;
        }
        return array;
    }

    public static Integer parseIntAsIntel(String hexString) {
        if (hexString.length() > 2) {
            String[] hexStrArray = hexStrToArray(hexString);
            String[] reverse     = ArrayUtil.reverse(hexStrArray);
            hexString = ArrayUtil.join(reverse, "");
        }
        return Integer.parseInt(hexString, 16);
    }

    public static String toHexAsIntel(Integer value) {
        String hexString = Integer.toHexString(value);
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }
        String[] hexStrArray = hexStrToArray(hexString);
        String[] reverse     = ArrayUtil.reverse(hexStrArray);
        return ArrayUtil.join(reverse, "");
    }
}